package main

import (
	"bytes"
	"fmt"
	"github.com/gin-gonic/gin"
	"os/exec"
	"toGraphqSDL/util"
)

func main() {
	r := gin.Default()
	r.GET("/sdl", func(c *gin.Context) {
		var url = c.Query("url")
		b := execShell("./downloadSDL.sh " + url)
		//c.Writer.Write(b.Bytes())
		bs := util.DoRewriteGraphql(b.Bytes(), "test")
		c.Header("content-disposition", `attachment; filename=`+"schema.graphql")
		c.Data(200, "\t\tc.Data(200, “application/octet-stream, b.Bytes())\n", bs)
	})
	r.Run("0.0.0.0:8086")
}

// 执行shell脚本
func execShell(s string) bytes.Buffer {
	cmd := exec.Command("/bin/bash", "-c", s)
	var out bytes.Buffer

	cmd.Stdout = &out
	err := cmd.Run()
	if err != nil {
		fmt.Println(err)
	}
	return out
}
